/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EkranWizyt.java
 *
 * Created on 2011-11-20, 22:56:55
 */
package przychodnia;

import DTO.Wizyty;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konp
 */
public class EkranWizyt extends javax.swing.JFrame
{

    private Session ses = null;
    //private Wizyty wizyty = null;
    List wizyty = null;

    /** Creates new form EkranWizyt */
    public EkranWizyt(Session xses)
    {
        ses = xses;
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        //getRootPane().setDefaultButton(okButton);

        wizyty = ses.createCriteria(Wizyty.class).list();

        refreshTable();
        
    /*            filterTextField.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {

                    wizyty = ses.createCriteria(Wizyty.class)
                            .add( Restrictions.or(
                            Restrictions.like("pacjent.nazwisko", "%" + filterTextField.getText() + "%"),
                            Restrictions.like("lekarz.nazwisko", "%" + filterTextField.getText() + "%")
                                    ))
                            .list();

                refreshTable();
            }
        });*/
        
        
                wizytyTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
        {

            public void valueChanged(ListSelectionEvent lse)
            {
                if (wizytyTable.getSelectedRow() < 0)
                {
                    cancelVisitButton.setEnabled(false);
                    editVisitButton.setEnabled(false);
                } else
                {
                    cancelVisitButton.setEnabled(true);
                    editVisitButton.setEnabled(true);
                }
            }
        });
    }

    private void refreshTable()
    {
        DefaultTableModel model = (DefaultTableModel) wizytyTable.getModel();
        model.setNumRows(0);
        wizyty = ses.createQuery("from Wizyty").list();
        model.setRowCount(wizyty.size());

        for (int i = 0; i < wizyty.size(); ++i)
        {
            Wizyty wizyta = (Wizyty) wizyty.get(i);
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

            model.setValueAt(wizyta.getId(), i, 0);
            model.setValueAt(formatter.format(wizyta.getData()), i, 1);
            model.setValueAt(wizyta.getPacjent().getNazwisko(), i, 2);
            model.setValueAt(wizyta.getPacjent().getImie(), i, 3);
            model.setValueAt(wizyta.getPacjent().getPESEL(), i, 4);
            model.setValueAt(wizyta.getLekarz().getNazwisko() + " " + wizyta.getLekarz().getImie(), i, 5);
            model.setValueAt(wizyta.getUwagi(), i, 6);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editVisitButton = new javax.swing.JButton();
        cancelVisitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        wizytyTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(przychodnia.Main.class).getContext().getResourceMap(EkranWizyt.class);
        editVisitButton.setText(resourceMap.getString("editVisitButton.text")); // NOI18N
        editVisitButton.setEnabled(false);
        editVisitButton.setName("editVisitButton"); // NOI18N
        editVisitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editVisitButtonActionPerformed(evt);
            }
        });

        cancelVisitButton.setText(resourceMap.getString("cancelVisitButton.text")); // NOI18N
        cancelVisitButton.setEnabled(false);
        cancelVisitButton.setName("cancelVisitButton"); // NOI18N
        cancelVisitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelVisitButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        wizytyTable.setAutoCreateRowSorter(true);
        wizytyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "Data wizyty", "Nazwisko pacjenta", "Imię pacjenta", "PESEL", "Lekarz", "Uwagi"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        wizytyTable.setName("wizytyTable"); // NOI18N
        jScrollPane1.setViewportView(wizytyTable);
        wizytyTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title6")); // NOI18N
        wizytyTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title0")); // NOI18N
        wizytyTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title1")); // NOI18N
        wizytyTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title2")); // NOI18N
        wizytyTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title3")); // NOI18N
        wizytyTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title4")); // NOI18N
        wizytyTable.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("wizytyTable.columnModel.title5")); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(editVisitButton)
                            .addGap(12, 12, 12)
                            .addComponent(cancelVisitButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(90, 90, 90)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(268, 268, 268))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelVisitButton)
                    .addComponent(cancelButton)
                    .addComponent(editVisitButton))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
        // TODO add your handling code here:

        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void editVisitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editVisitButtonActionPerformed
    {//GEN-HEADEREND:event_editVisitButtonActionPerformed
        EkranEdycjiWizyt jf = new EkranEdycjiWizyt(ses, (Long) wizytyTable.getValueAt(wizytyTable.getSelectedRow(), 0), 1);
        JFrame mainFrame = Main.getApplication().getMainFrame();
        jf.setLocationRelativeTo(mainFrame);
        Main.getApplication().show(jf);
    }//GEN-LAST:event_editVisitButtonActionPerformed

    private void cancelVisitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelVisitButtonActionPerformed
    {//GEN-HEADEREND:event_cancelVisitButtonActionPerformed
        Long id = (Long)wizytyTable.getValueAt(wizytyTable.getSelectedRow(), 0);
        Wizyty wizyta = (Wizyty) ses.createCriteria(Wizyty.class).add(Restrictions.eq("id", id)).list().get(0);
        
        ses.beginTransaction();
        ses.delete(wizyta);
        ses.getTransaction().commit();
    }//GEN-LAST:event_cancelVisitButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        refreshTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(EkranWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(EkranWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(EkranWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(EkranWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                //   new EkranWizyt().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton cancelVisitButton;
    private javax.swing.JButton editVisitButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable wizytyTable;
    // End of variables declaration//GEN-END:variables
}
